package com.ttdevs.es.service;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.os.Vibrator;

import com.ttdevs.util.BaseClass;

public class MainAlarmManager extends BaseClass {

	private final static long[] pattern = { 100, 300 };

	private Context context;
	private Handler handler;

	private Vibrator vibrator;
	private MediaPlayer mediaPlayer;
	private AudioManager audioManager;
	private int maxVolume;
	private int curVolume;
	private int ringVolume;
	private String ringSrc;

	private boolean isCheckVolume = false;

	public MainAlarmManager(Context context, Handler handler) {
		super(context, handler);
		this.context = context;
		this.handler = handler;

		ringVolume = getSPU().getRingVolume();
		ringSrc = getSPU().getRingSrc();

		vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);// 震动
		audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);// 音量
		audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, ringVolume, 0);
		curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);// 获取当前音量，供解除时恢复之�?
		maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);// 获取�?��音量,当前音量

		// 插入耳机外放暂时无效
		audioManager.setMicrophoneMute(false);
		audioManager.setSpeakerphoneOn(true);
	}

	public void start() {
		getSPU().putRunning(true);
		if (getSPU().getVibrate()) {
			vibrator.vibrate(pattern, 0);
			context.registerReceiver(this, new IntentFilter(Intent.ACTION_SCREEN_OFF));
		}
		if (getSPU().getRing()) {
			isCheckVolume = true;
			checkVolumeChange();

			mediaPlayer = MediaPlayer.create(context, Uri.parse(ringSrc));
			mediaPlayer.setLooping(true);
			mediaPlayer.start();
		}
	}

	public void stop() {
		if (!getSPU().getRunning()) {
			return;
		}
		getSPU().putRunning(false);
		if (getSPU().getVibrate()) {
			vibrator.cancel();
			context.unregisterReceiver(this);
		}
		if (getSPU().getRing()) {
			isCheckVolume = false;
			audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, curVolume, 0);

			if (mediaPlayer != null) {
				mediaPlayer.stop();
				mediaPlayer.release();
				mediaPlayer = null;
			}
		}
	}

	private void checkVolumeChange() {
		handler.postDelayed(new Runnable() {

			@Override
			public void run() {
				if (ringVolume != audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)) {
					audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, 0);
					ringVolume = maxVolume;
				}
				if (isCheckVolume) {
					handler.postDelayed(this, 500L);
				}
			}
		}, 500L);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);
		if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
			if (getSPU().getVibrate()) {
				vibrator.vibrate(pattern, 0);
			}
		}
	}
}
